package org.dingyom.basics.controller;

import org.apache.commons.io.FilenameUtils;
import org.dingyom.basics.service.IFastDfsService;
import org.dingyom.basics.util.AjaxResult;
import org.dingyom.basics.util.FastdfsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    @Autowired
    IFastDfsService fastDfsService;
    @PostMapping
    public AjaxResult upload(MultipartFile file){
        try {
            byte[] bytes = file.getBytes();
            String originalFilename = file.getOriginalFilename();
            String extension = FilenameUtils.getExtension(originalFilename);//文件拓展名
            String path = fastDfsService.upload(bytes,extension);
            return AjaxResult.me().setData(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传失败"+e.getMessage());
        }
    }

    @DeleteMapping
    public AjaxResult delete(String path){
        try {
            fastDfsService.delete(path);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }
}
